/* eslint-disable max-lines */
import * as BASE from '@/stores/definitions-base';
import { Input, NumberPicker, Switch } from '@alifd/next';
import { t } from '@lingui/macro';
import React from 'react';
import DataSelect from '@/components/DataSelect';
import { getCity } from './api';
import TauSimpleSelect from '@/components/TauSimpleSelect';
import { DictItemSelect } from '@/components/biz';
import { ellipsisRender } from '@/components/TauTable';
import DictItemExtrasSelect from '@/components/biz/DictItemExtrasSelect';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

/**
 * @param {*} path        属性path
 * @param {*} props       column属性
 * @returns column定义
 */
export function column(...args) {
  return columnBase(xcxModal, ...args);
}

/**
 * 显示一组表单字段
 * ```js
 * {
 *   fields: string[],    // 字段名称，可以使用.来指定嵌套对象属性，例如order.busOrder.shift
 *   columns: number,     // 列数，默认为4,最大为12,最小为1
 *   model: object,       // 字段定义数据模型的根对象，默认不指定，仅在使用子对象作为根模型时设置
 *   overrideModel: object, // 重载字段定义数据模型
 *   mode: string,        // 表单模式，默认为edit，可选view
 *   itemLayout: object,  // FormItem布局配置，默认为{}
 * }
 * ```
 *
 * @param {object} param0
 * @returns
 */
export function FieldsGrid(props) {
  return <FieldGridBase model={xcxModal} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  // <br/> 0 - disabled; 1 - enabled
  status: {
    1: true,
    0: false,
  },
  type: {
    1: t`包车`,
    2: t`专线`,
    3: t`巴士`,
    4: t`拼车`,
    5: t`日租包车`,
  },
  subBusinessType: {
    1: t`包车`,
    2: t`专线`,
    3: t`巴士`,
    4: t`拼车`,
  },
  lineType: [
    { label: t`包车`, value: 1 },
    { label: t`专线`, value: 2 },
    { label: t`巴士`, value: 3 },
    { label: t`拼车`, value: 4 },
  ],
  jumpType: [
    { label: t`跳转小程序指定页面`, value: 0 },
    { label: t`打开活动弹窗`, value: 1 },
  ],
  subType: {
    1: t`单程包车`,
    4: t`本地包车`,
    // 5: t`日租包车`,
    2: t`接送机`,
  },
  appType: {
    'cbv-mzd': t`包车小程序`,
    // 'cbv-bus': t`巴士小程序`,
  },
  entryType: {
    0: t`普通入口`,
    1: t`热门线路入口`,
    2: t`轮播图入口`,
  },

  tabItems: [
    { key: '0', label: t`普通入口` },
    { key: '1', label: t`热门线路入口` },
    { key: '2', label: t`轮播图入口` },
    { key: '3', label: t`拼车线路入口` },
    { key: '5', label: t`入口位置` },
    { key: '6', label: t`入口Logo` },
  ],

  _remove_text: {
    0: t`确认删除该普通入口吗？`,
    1: t`确认删除该热门线路入口吗？`,
    2: t`确认删除该轮播图入口吗？`,
    3: t`确认删除该拼车线路入口吗？`,
    5: t`确认删除该入口位置吗？`,
    6: t`确认删除该入口Logo吗？`,
  },

  EDITNAME: {
    0: 'edit',
    1: 'edit',
    2: 'edit',
    3: 'edit',
    5: 'entrance',
    6: 'logo',
  },

  VIEWNAME: {
    5: 'viewEntrance',
    6: 'viewLogo',
  },

  _cc_keys: [
    { key: 'scPropagationLanguage', title: '专线宣传语' },
    { key: 'busPropagationLanguage', title: '巴士宣传语' },
    { key: 'singleCcPropagationLanguage', title: '单程包车宣传语' },
    { key: 'pickCcPropagationLanguage', title: '接送机包车宣传语' },
    { key: 'localCarPropagationLanguage', title: '接送机包车宣传语' },
    { key: 'singleCcSpecialLanguage', title: '单程包车特殊宣传语' },
    { key: 'pickCcSpecialLanguage', title: '接送机包车特殊宣传语' },
    { key: 'scSpecialLanguage', title: '专线特殊宣传语' },
    { key: 'busSpecialLanguage', title: '巴士特殊宣传语' },
    { key: 'localCarSpecialLanguage', title: '本地用车特殊宣传语' },
    { key: 'stationPropagationLanguage', title: '拼车始发站-到达站宣传语' },
    { key: 'rsPlayOrderCautionLanguage', title: '拼车下单界面注意事项' },
    { key: 'rsOrderDetailCautionLanguage', title: '拼车订单详情界面注意事项' },
    { key: 'appTitle', title: '标题' },
    { key: 'themeColor', title: '主题色' },
  ],

  _bus_keys: [
    { key: 'scPropagationLanguage', title: '专线宣传语' },
    { key: 'busPropagationLanguage', title: '巴士宣传语' },
    { key: 'scSpecialLanguage', title: '专线特殊宣传语' },
    { key: 'busSpecialLanguage', title: '巴士特殊宣传语' },
    { key: 'stationPropagationLanguage', title: '拼车始发站-到达站宣传语' },
    { key: 'rsPlayOrderCautionLanguage', title: '拼车下单界面注意事项' },
    { key: 'rsOrderDetailCautionLanguage', title: '拼车订单详情界面注意事项' },
    { key: 'appTitle', title: '标题' },
    { key: 'themeColor', title: '主题色' },
  ],

};

/**
 * 模型定义：
 * 1. 普通文本仅需要定义label
 * 2. 需要通过枚举转换的值，定义text; 当使用renderer返回组件时，text属性无效，需要通过组件的renderPreview来指定预览时的绘制方式
 * 3. 通用的ui属性需要定制时，定义ui; 特定模式(view, edit, add)下的ui，添加后缀定义为ui_<mode>，例如：ui_view, ui_edit, ui_add, ui_table
 * 4. ui可配置属性包括:
 *    renderer: 内容渲染函数, Form表单中的原型为：(value: any, formData: any, fieldDefinition: any) => ReactNode; Table Column中的原型为: (value: any, columnIndex: number, rowData: any) => ReactNode;
 *    itemProps: 表单Form.Item的属性，可以是对象，或函数({ isPreview: boolean, mode: string, value: any, formData: any, ...otherDefinitions}) => void;
 *    span: Grid Cell占用的列数, 默认为1
 *
 * 【注】获取接口的model定义后，vscode中通过正则替换：^(\s+)"label": "([^']+)"  ===> $1label: t`$2`
 */
export const xcxModal = {
  id: {
    label: t`标识`,
  },
  enable: {
    label: t`状态`,
    ui_table: {
      renderer: (v, i, r) => <Switch checkedChildren="启用" unCheckedChildren="禁用" autoWidth checked={v} size="small" onClick={() => rootStore.xcxStore.updateEnable(r)} />,
    },
    ui: {
      renderer: (v) => <Switch checkedChildren="启用" unCheckedChildren="禁用" autoWidth checked={v} size="small" />,
    },
  },
  routeName: {
    label: t`入口名称`,
    text: (v) => <>{ellipsisRender(v)}</>,
    ui: {
      itemProps: { required: true },
    },
  },
  type: {
    label: t`线路类型`,
    text: (v) => ENUMS.type[v],
    ui: {
      renderer: (v) => <TauSimpleSelect value={v} dataSource={toSelectDataSource(ENUMS.type)} />,
    },
  },
  entryType: {
    label: t`入口类型`,
    ui: {
      renderer: (v) => <TauSimpleSelect value={v} dataSource={toSelectDataSource(ENUMS.entryType)} />,
      itemProps: { required: true },
    },
  },
  subBusinessType: {
    label: t`业务类型`,
    ui: {
      renderer: (v) => <TauSimpleSelect value={v} mode="multiple" dataSource={toSelectDataSource(ENUMS.subBusinessType)} />,
    },
  },
  mainTitle: {
    label: t`主标题`,
    ui: {
      itemProps: {
        required: true,
      },
    },
  },
  subTitle: {
    label: t`副标题`,
  },
  subType: {
    label: t`包车类型`,
    ui: {
      renderer: (v) => <DictItemSelect value={v} dataSource={toSelectDataSource(ENUMS.subType)} />,
    },
  },
  appType: {
    label: t`小程序类型`,
    text: (v) => {
      if (!v) return <></>;
      return (
        <>
          {v.map((p) => (
            <div>{ENUMS.appType[p]}</div>
          ))}
        </>
      );
    },
    ui: {
      renderer: (v) => <TauSimpleSelect mode="multiple" value={v} dataSource={toSelectDataSource(ENUMS.appType)} />,
      itemProps: { required: true },
    },
  },
  showPalace: {
    label: t`展示位置`,
    ui: {
      renderer: (v) => <DictItemSelect mode="multiple" dictId="travelPagePath" value={v} />,
    },
  },
  iconName: {
    label: t`icon名称`,
  },
  hotRouteName: {
    label: t`热门线路名称`,
  },
  link: {
    label: t`链接`,
  },
  appId: {
    label: t`小程序appId`,
  },
  sortWeight: {
    label: t`权重`,
    ui: {
      renderer: (v) => <NumberPicker value={v} />,
    },
  },
  startMainArea: {
    label: t`开始城市`,
    ui: {
      renderer: (v) => <DataSelect hasClear onFetch={() => getCity('region')} value={v} labelField="name" valueField="itemId" />,
      itemProps: { required: true },
    },
  },
  startMainAreaText: {
    label: t`开始城市`,
  },
  startMinorAreas: {
    label: t`开始站点`,
  },
  startMinorAreasText: {
    label: t`开始站点`,
    text: (v) => <>{ellipsisRender(v)}</>,
  },
  endMainArea: {
    label: t`结束城市`,
    ui: {
      renderer: (v) => <DataSelect hasClear onFetch={() => getCity('region')} value={v} labelField="name" valueField="itemId" />,
      itemProps: { required: true },
    },
  },
  endMainAreaText: {
    label: t`结束城市`,
  },
  endMinorAreas: {
    label: t`结束站点`,
  },
  endMinorAreasText: {
    label: t`结束站点`,
    text: (v) => <>{ellipsisRender(v)}</>,
  },
  description: {
    label: t`说明`,
    ui: {
      renderer: (v) => <Input.TextArea value={v} rows={6} />,
      span: 4,
    },
  },
  imgConfigs: {
    label: t`图片`,
    name: {
      label: t`名称`,
    },
    type: {
      label: t`格式`,
    },
    uid: {
      label: t`文件id`,
    },
  },
  imageText: {
    label: t`图片文字`,
  },
  crBy: {
    label: t`创建者`,
  },
  upBy: {
    label: t`最后修改者`,
  },
  crDate: {
    label: t`创建时间`,
    text: DATETIME_TEXT,
  },
  upDate: {
    label: t`最后修收时间`,
  },
  tenantId: {
    label: t`租户ID`,
  },

  // 字典模型  前端新增
  name: {
    label: t`名称`,
  },
  itemId: {
    label: t`标识`,
    ui_edit: {
      renderer: (v) => <span>{v}</span>,
    },
  },
  disabled: {
    label: t`禁用`,
    text: (v) => (v ? t`是` : t`否`),
  },
  extras: {
    label: t`自定义属性`,
  },
  iconUrl: {
    label: t`入口icon名称`,
  },
  jumpType: {
    label: t`轮播图跳转类型`,
    ui: {
      renderer: (v) => <DictItemExtrasSelect value={v} dictId="sys" itemId="swiperJumpType" />,
      itemProps: {
        required: true,
      },
    },
  },
  lineType: {
    label: t`线路类型`,
    ui: {
      renderer: (v) => <TauSimpleSelect value={v} data={toSelectDataSource(ENUMS.type)} />,
    },
  },
};
